/*******************************************************************************
 * This file is part of blueprint4j www.blueprint4j.org.
 * 
 *     blueprint4j is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU Lesser General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 * 
 *     blueprint4j is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY; without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU Lesser General Public License for more details.
 * 
 *     You should have received a copy of the GNU Lesser General Public License
 *     along with blueprint4j. If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/
package blueprint4j.gui;

/**
 * This Class represetnts a progress wizard. It has a previous and next button, and displays
 * a WizardPanel
 */
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Wizard implements ActionListener {
	private AbstractWizardPanel wizard_panel = null;
	private JButton next_button = null;
	private JButton previous_button = null;
	private JButton cancel_button = null;
	private JDialog dialog = null;
	private JFrame owner = null;
	private String title = null;
	private JPanel heading_panel = null;
	private JPanel display_panel = null;
	private JPanel control_panel = null;
	//private JScrollPane scroll_pane = null;
	private int size_x = 0;
	private int size_y = 0;
	public boolean sucessfully_completed = false;

	public Wizard(JFrame p_owner, String p_title, AbstractWizardPanel p_wizard_panel) {
		owner = p_owner;
		wizard_panel = p_wizard_panel;
		next_button = new JButton("Next");
		next_button.addActionListener(this);
		previous_button = new JButton("Previous");
		previous_button.addActionListener(this);
		cancel_button = new JButton("Cancel");
		cancel_button.addActionListener(this);
		title = p_title;
	}

	public Wizard(JFrame p_owner, String p_title, AbstractWizardPanel p_wizard_panel, int p_size_x, int p_size_y) {
		this(p_owner, p_title, p_wizard_panel);
		size_x = p_size_x;
		size_y = p_size_y;
	}

	/**
	 * Displays a modal frame
	 */
	public void showWizard() {
		dialog = new JDialog(owner, title, true);
		dialog.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
		dialog.addWindowListener(new WindowAdapter() {
					public void windowClosing(WindowEvent e) {
						doClose();
					}
				}
										);
		heading_panel = new JPanel();
		JLabel heading_label = new JLabel(title);
		heading_label.setFont(new Font("Arial", Font.BOLD, 20));
		heading_panel.add(heading_label, BorderLayout.CENTER);
		display_panel = new JPanel();
		display_panel.add(wizard_panel);
		control_panel = new JPanel();
		control_panel.add(cancel_button, BorderLayout.WEST);
		control_panel.add(previous_button, BorderLayout.CENTER);
		control_panel.add(next_button, BorderLayout.EAST);
		dialog.getContentPane().add(heading_panel, BorderLayout.NORTH);
		dialog.getContentPane().add(display_panel, BorderLayout.CENTER);
		dialog.getContentPane().add(control_panel, BorderLayout.SOUTH);
		dialog.pack();
		if (size_x == 0) {
			size_x = dialog.getSize().width;
		}
		if (size_y == 0) {
			size_y = dialog.getSize().height;
		}
		dialog.setSize(new Dimension(size_x, size_y));
		dialog.setLocationRelativeTo(null);
		refresh();
	}

	private void refresh() {
		display_panel.removeAll();
		display_panel.add(wizard_panel);
		if (wizard_panel.isLastPanel()) {
			next_button.setText("Done");
		} else {
			next_button.setText("Next");
		}
		if (wizard_panel.isFirstPanel()) {
			previous_button.setEnabled(false);
		} else {
			previous_button.setEnabled(true);
		}
		dialog.repaint();
		dialog.setVisible(true);
	}

	private void doClose() {
		if (JOptionPane.showConfirmDialog(dialog, "Are you sure you want to exit?","Exit",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
			dialog.dispose();
		}
	}

	public void actionPerformed(ActionEvent action_event) {
		if (action_event.getSource() == cancel_button) {
			doClose();
		}
		if (action_event.getSource() == previous_button) {
			try {
				AbstractWizardPanel previous_panel = wizard_panel.getPreviousPanel();
				wizard_panel.getPreviousPanelEvent();
				if (wizard_panel.getPreviousPanel() == null) {
					JOptionPane.showMessageDialog(dialog, "There is no previous frame", "Navigation Error", JOptionPane.WARNING_MESSAGE);
				} else {
					wizard_panel = wizard_panel.getPreviousPanel();
					wizard_panel.reactivatedEvent();
					refresh();
				}
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(dialog, ex.getMessage(), "An error has occurred", JOptionPane.ERROR_MESSAGE);
			}
		}
		if (action_event.getSource() == next_button) {
			try {
				AbstractWizardPanel next_panel = wizard_panel.getNextPanel();
				if (wizard_panel.isLastPanel()) {
					sucessfully_completed = true;
					JOptionPane.showMessageDialog(dialog, title + " sucessfully completed");
					dialog.dispose();
					return ;
				}
				next_panel.setPreviousPanel(wizard_panel);
				if (next_panel == null) {
					//JOptionPane.showMessageDialog(dialog,"There is no next frame","Navigation Error",JOptionPane.WARNING_MESSAGE);
				}
				else {
					wizard_panel = next_panel;
					refresh();
				}
			} catch (Exception ex) {
				JOptionPane.showMessageDialog(dialog, ex.getMessage(), "An error has occurred", JOptionPane.ERROR_MESSAGE);
			}
		}
	}
}
